package com.mybatis.demo;

import com.mybatis.demo.domain.User;
import com.mybatis.demo.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/8/14 20:44
 * @Description
 */
public class ReturnTest {
    public static void main(String[] args) throws IOException {
        // 1、加载配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        // 2、创建 SqlSessionFactoryBuilder 对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 3、创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = builder.build(is);
        // 4、创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5、获取 UserMapper 接口
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 6、调用方法
//        Integer count = userMapper.countNum();
//        System.out.println(count);
//        User user = userMapper.getOneUser();
//        System.out.println(user);
//        List<User> userList = userMapper.queryList();
//        for (User user : userList) {
//            System.out.println(user);
//        }
//        Map<String, Object> res = userMapper.getMapRes();
//        System.out.println(res);
//        List<Map<String, Object>> mapList = userMapper.getAllMapRes();
//        for (Map<String, Object> ans : mapList) {
//            System.out.println(ans);
//        }
        Map<String, Object> mapResult = userMapper.getAllMapKey();
        System.out.println(mapResult);
        // 7、关闭流
        is.close();
    }
}
